package net.ecpt.dubbo.clm;

import java.util.HashMap;
import java.util.Map;

import org.ofbiz.base.util.Debug;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.DelegatorFactory;
import org.ofbiz.service.GenericServiceException;
import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.ServiceDispatcher;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class ClmServiceImpl implements ClmService {
    @Override
    public JSONObject getClmAgreement(JSONObject json) {
	Delegator delegator = DelegatorFactory.getDelegator("default");
	LocalDispatcher dispatcher = ServiceDispatcher.getLocalDispatcher("default", delegator);
	Map<String, Object> result = new HashMap<String, Object>();
	try {
	    Map<String,Object> jsonToMap = JSONObject.parseObject(json.toJSONString()); 
	    result = dispatcher.runSync("getClmAgreementPrice", jsonToMap);
	} catch (GenericServiceException e) {
	    e.printStackTrace();
	}
	JSONObject JSONObj = JSONObject.parseObject(JSON.toJSONString(result));
        return JSONObj;
    }
    
    
    public JSONObject listClmAgreement(JSONObject json) {
	Delegator delegator = DelegatorFactory.getDelegator("default");
	LocalDispatcher dispatcher = ServiceDispatcher.getLocalDispatcher("default", delegator);
	Map<String, Object> result = new HashMap<String, Object>();
	try {
	    Map<String,Object> jsonToMap = JSONObject.parseObject(json.toJSONString()); 
	    result = dispatcher.runSync("listClmAgreement", jsonToMap);
	} catch (GenericServiceException e) {
	    e.printStackTrace();
	}
	JSONObject JSONObj = JSONObject.parseObject(JSON.toJSONString(result));
        return JSONObj;
    }
    
    
    public JSONObject createClmCollectionOrder(JSONObject json) {
	Delegator delegator = DelegatorFactory.getDelegator("default");
	LocalDispatcher dispatcher = ServiceDispatcher.getLocalDispatcher("default", delegator);
	Map<String, Object> result = new HashMap<String, Object>();
	try {
	    Map<String,Object> jsonToMap = JSONObject.parseObject(json.toJSONString()); 
	    jsonToMap = ClmUtils.optimizingResults(jsonToMap, "ClmCollectionOrder");
	    Debug.log("jsonToMap==="+jsonToMap);
	    result = dispatcher.runSync("createClmCollectionOrder", jsonToMap);
	} catch (GenericServiceException e) {
	    e.printStackTrace();
	}
	JSONObject JSONObj = JSONObject.parseObject(JSON.toJSONString(result));
        return JSONObj;
    }
}
